﻿layui.use(['layer', 'table', 'laydate', 'laypage', 'jquery'], function () {
    var $ = layui.jquery,
        table = layui.table,
        player = parent.layer || layui.layer,
        laydate = layui.laydate;

    var projectInfoListLogic = {
        //初始化
        init: function () {
            self = this;
            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                field: 'StartDate',
                order: 'desc',
                listsUrl: "/Project/Info/LoadListInfo",
                addUrl: "/Project/Info/Add",
                modifyUrl: "/Project/Info/Modify",
                removeUrl: "/Project/Info/Remove"
            };

            //this.Parameters.ac
            //    .pushQueue(function () {
            //        CommonLogic.loadButtonsInfo(function () {
            //            self.Parameters.ac.notifyQueueAsyncFuncComplate();
            //        })
            //    })
            //    .pushQueue(function () {
            //        self.initPage();
            //        self.Parameters.ac.notifyQueueAsyncFuncComplate();
            //    })
            //    .pushQueue(this.bindEvent)
            //    .exec();

            //页面初始化
            this.initPage();
            //时间初始化
            this.bindEvent();
        },
        //初始化列表
        initPage: function () {
            //日期
            var startTime = laydate.render({
                elem: '#txtBeginTime',
                done: function (value, date, endDate) {
                    endLayDate.config.min = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                        /* hours: date.hours,
                           minutes: date.minutes,
                           seconds: date.seconds +1*/
                    };
                }
            });

            var endLayDate = laydate.render({
                elem: '#txtEndTime',
                done: function (value, date, endDate) {
                    startTime.config.max = {
                        year: date.year != undefined ? date.year : 2099,
                        month: (date.month != undefined) ? (date.month - 1) : 12,
                        date: (date.date != undefined) ? date.date : 25
                        /* hours: date.hours,
                           minutes: date.minutes,
                           seconds: date.seconds -1*/
                    };
                }
            });

            var title =
                [{ type: 'checkbox', fixed: 'left' },
                { field: 'ID', title: 'ID', hide: true },
                { field: 'ProjectName', title: '工程名称', width: 180, align: "center", sort: true, fixed: 'left' },
                    { field: 'ProjectNo', title: '工程编码', width: 180, align: "center", sort: true, fixed: 'left' },
                {
                    field: 'StartDate', title: '开工日期', width: 140, align: "center", sort: true, fixed: 'left',
                    templet: function (d) {
                        return GrdAssist.formatDate(d.StartDate, "yyyy-MM-dd")
                    }
                },
                {
                    field: 'EndDate', title: '结束日期', width: 140, align: "center", sort: true, fixed: 'left',
                    templet: function (d) {
                        return GrdAssist.formatDate(d.EndDate, "yyyy-MM-dd")
                    }
                },
                { field: 'ProjectType', title: '工程类型', width: 120, align: "center", fixed: 'left' },
                { field: 'ContractName', title: '承包性质', width: 120, align: "center", fixed: 'left' },
                {
                    field: 'CheckZT', title: '状态', width: 80, align: "center",
                    templet: function (d) {
                        return GrdEnum.getValue.checkStatusValue(d.CheckZT);
                    }
                },
                { field: 'ProjectArea', title: '件数面积', width: 120 },
                { field: 'ContractMoney', title: '合同金额', width: 120 },
                { field: 'AuditMoney', title: '结算金额', width: 120 },
                { field: 'Charge', title: '负责人', width: 120 },
                { field: 'Phone', title: '联系电话', width: 120 },
                //{ field: 'SJDW', title: '设计单位', width: 120 },
                //{ field: 'JSDW', title: '结算单位', width: 120 },
                //{ field: 'SGDW', title: '施工单位', width: 120 },

                { field: 'opt', title: '操作', toolbar: '#gridTool', width: '150', align: 'left', fixed: 'right' }
                ];

            //table加载
            GrdLayUI.table({
                dom: '#InfoList',
                height: "full-140",
                url: self.Parameters.listsUrl,
                field: self.Parameters.field,
                order: self.Parameters.order,
                parameters: {
                    sKeyWords: $("#txtKeyWord").val(),
                    dBeginTime: $("#txtBeginTime").val(),
                    dEndTime: $("#txtEndTime").val()
                },
                title: title
            });

            //排序监听
            GrdLayUI.tableSort({
                domId: "InfoList",
                backAction: function (obj) {
                    self.Parameters.field = obj.field;
                    self.Parameters.order = obj.type;
                    self.logicFunc.loadListInfo();
                }
            });
        },
        //绑定事件
        bindEvent: function () {
            //监听(列表操作)
            table.on('tool(InfoList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "funBtnEdit":
                        self.logicFunc.modifyInfo(data);
                        break;
                    case "funBtnRemove":
                        self.logicFunc.removeInfo(data);
                        break;
                }
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });

            //新增
            $("#funBtnAdd").on("click", function () {
                self.logicFunc.addInfo();
            });

            //修改
            $("#funBtnEdit").on("click", function () {
                self.logicFunc.modifyInfo();
            });

            //删除
            $("#funBtnRemove").on("click", function () {
                self.logicFunc.removeInfo();
            });


        },
        //逻辑方法
        logicFunc: {

            ///获取数据
            loadListInfo: function () {
                GrdLayUI.tableReload({
                    domId: "InfoList",
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: {
                        sKeyWords: $("#txtKeyWord").val(),
                        dBeginTime: $("#txtBeginTime").val(),
                        dEndTime: $("#txtEndTime").val()
                    }
                });
            },

            //增加按钮信息
            addInfo: function () {
                var index = layui.layer.open({
                    title: "增加项目信息",
                    type: 2,
                    resize: false,
                    area: ['1050px', '620px'],
                    content: self.Parameters.addUrl,
                    end: function () {
                        $(window).unbind("resize");
                    }
                });
                //layui.layer.full(index);
            },

            //修改按钮信息
            modifyInfo: function (data) {
                if (data != undefined && data != null) {
                    var index = layui.layer.open({
                        title: "修改项目信息",
                        type: 2,
                        resize: false,
                        area: ['1050px', '620px'],
                        content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                } else {
                    GrdLayUI.getSingleCheckData({
                        domId: "InfoList",
                        backAction: function (data) {
                            var index = layui.layer.open({
                                title: "修改项目信息",
                                type: 2,
                                resize: false,
                                area: ['1050px', '620px'],
                                content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                                end: function () {
                                    $(window).unbind("resize");
                                }
                            });
                        }
                    });
                }
            },

            //删除按钮信息
            removeInfo: function (data) {
                if (data != undefined && data != null) {
                    GrdLayUI.removeDataByIdInfo({
                        data: data,
                        removeUrl: self.Parameters.removeUrl,
                        backAction: function (data) {
                            self.logicFunc.loadListInfo();
                        }
                    });
                } else {
                    GrdLayUI.removeDataById({
                        domId: "InfoList",
                        removeUrl: self.Parameters.removeUrl,
                        backAction: function (data) {
                            self.logicFunc.loadListInfo();
                        }
                    });
                }
            }
        },
        //工具
        tools: {

        }
    };
    projectInfoListLogic.init();
});
